package cn.itcast.openai;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

/**
 * 简单对话Demo
 * @author zgp
 * @version V1.0
 * @date 2025-04-19 16:42
 */
public class CompletionsDemo {
    public static void main(String[] args) {
        // 构建客户端
        OpenAIClient client = OpenAIOkHttpClient.builder()
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .apiKey(System.getenv("ALIYUN_API_KEY"))
                .build();
        // 构建聊天参数
        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .model("qwen-turbo")
                .addSystemMessage("你是一个Java开发助手，名字叫小智。")
                .addUserMessage("你好，你是谁？")
                .build();
        // 发送请求，获取结果
        client.chat().completions().create(params)
                .choices()
                .stream()
                // .map(choice -> choice.message().content().get())
                .flatMap(choice -> choice.message().content().stream())
                .forEach(System.out::println);
    }
}
